草庐IT

C++ union 与 reinterpret_cast

全部标签

c++ - 如何在不破坏代码的情况下正确修复 "zero-sized array in struct/union"警告(C4200)?

我正在将一些代码集成到我的库中。这是一个针对速度进行了优化的复杂数据结构,所以我尽量不要对其进行过多修改。集成过程进展顺利,实际上几乎完成(它编译)。一件事仍然困扰着我。我多次收到C4200警告:warningC4200:nonstandardextensionused:zero-sizedarrayinstruct/unionCannotgeneratecopy-ctororcopy-assignmentoperatorwhenUDTcontainsazero-sizedarray代码有效,但这个警告让我毛骨悚然(尤其是带有copy-ctor的部分)。由于这样声明的结构而出现警告:#

c++ - 如何在不破坏代码的情况下正确修复 "zero-sized array in struct/union"警告(C4200)?

我正在将一些代码集成到我的库中。这是一个针对速度进行了优化的复杂数据结构,所以我尽量不要对其进行过多修改。集成过程进展顺利,实际上几乎完成(它编译)。一件事仍然困扰着我。我多次收到C4200警告:warningC4200:nonstandardextensionused:zero-sizedarrayinstruct/unionCannotgeneratecopy-ctororcopy-assignmentoperatorwhenUDTcontainsazero-sizedarray代码有效,但这个警告让我毛骨悚然(尤其是带有copy-ctor的部分)。由于这样声明的结构而出现警告:#

c++ - 如何避免 const cast 进行 map 访问?

我有以下问题:std::mapmap;voidgetColor(Aconst*obj){doubled=map[obj];//doesnotcompilewihtoutconst_cast(obj)//dosomething}我有一个mapstd::map(某处),它存储指向对象A的指针。我有一个getColor函数,它不操作对象A,因此将指向constA的指针作为输入.如果不使用const_cast,函数getColor将无法编译。constcast是一个设计问题,但如果我不想在map中制作键,我不知道如何规避它常量。任何帮助表示赞赏。 最佳答案

c++ - 如何避免 const cast 进行 map 访问?

我有以下问题:std::mapmap;voidgetColor(Aconst*obj){doubled=map[obj];//doesnotcompilewihtoutconst_cast(obj)//dosomething}我有一个mapstd::map(某处),它存储指向对象A的指针。我有一个getColor函数,它不操作对象A,因此将指向constA的指针作为输入.如果不使用const_cast,函数getColor将无法编译。constcast是一个设计问题,但如果我不想在map中制作键,我不知道如何规避它常量。任何帮助表示赞赏。 最佳答案

c++ - 在 C++ 中,static_cast<double>(a) 和 double(a) 有什么区别?

两者有什么区别inta;//agetssomevaluedoublepi=static_cast(a)/3;和inta;//agetssomevaluedoublepi=double(a)/3;你见过后者吗?在我看来,我在Stroustrup编写的一些片段中看到了它,但我找不到引用。 最佳答案 可能有人认为他们是在构建而不是在类型转换。考虑:some_fun(std::string("Hello"));许多人认为他们在那里调用了构造函数,而实际上他们正在执行C风格的转换。碰巧的是,强制转换会在它查看的一长串其他东西中查看目标类型的构

c++ - 在 C++ 中,static_cast<double>(a) 和 double(a) 有什么区别?

两者有什么区别inta;//agetssomevaluedoublepi=static_cast(a)/3;和inta;//agetssomevaluedoublepi=double(a)/3;你见过后者吗?在我看来,我在Stroustrup编写的一些片段中看到了它,但我找不到引用。 最佳答案 可能有人认为他们是在构建而不是在类型转换。考虑:some_fun(std::string("Hello"));许多人认为他们在那里调用了构造函数,而实际上他们正在执行C风格的转换。碰巧的是,强制转换会在它查看的一长串其他东西中查看目标类型的构

c++ - union 成员有一个重要的复制构造函数

这个问题在这里已经有了答案:Whycompilerdoesn'tallowstd::stringinsideunion?(7个回答)关闭6年前。我有一个如下所示的union:union{intintValue;doubledoubleValue;std::stringstringValue;void*pointerValue;}values;当我编译它时,我得到了这个错误信息(是的,我做了#include):./Value.hh:19:19:error:unionmember'stringValue'hasanon-trivialcopyconstructorstd::stringst

c++ - union 成员有一个重要的复制构造函数

这个问题在这里已经有了答案:Whycompilerdoesn'tallowstd::stringinsideunion?(7个回答)关闭6年前。我有一个如下所示的union:union{intintValue;doubledoubleValue;std::stringstringValue;void*pointerValue;}values;当我编译它时,我得到了这个错误信息(是的,我做了#include):./Value.hh:19:19:error:unionmember'stringValue'hasanon-trivialcopyconstructorstd::stringst

c++ - 具有 const 和 nonconst 成员的 union ?

这似乎是未定义的行为unionA{intconstx;floaty;};Aa={0};a.y=1;规范说Creatinganewobjectatthestoragelocationthataconstobjectwithstatic,thread,orautomaticstoragedurationoccupiesor,atthestoragelocationthatsuchaconstobjectusedtooccupybeforeitslifetimeendedresultsinundefinedbehavior.但是没有编译器会警告我,因为它很容易诊断错误。我是否误解了措辞?

c++ - 具有 const 和 nonconst 成员的 union ?

这似乎是未定义的行为unionA{intconstx;floaty;};Aa={0};a.y=1;规范说Creatinganewobjectatthestoragelocationthataconstobjectwithstatic,thread,orautomaticstoragedurationoccupiesor,atthestoragelocationthatsuchaconstobjectusedtooccupybeforeitslifetimeendedresultsinundefinedbehavior.但是没有编译器会警告我,因为它很容易诊断错误。我是否误解了措辞?